<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" th:href="@{favicon.ico}" type="image/x-icon"/>
    <title>业务管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
     <!-- 引入Bootstrap JS和Popper.js -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        :root {
            --primary-color: #3a7bd5;
            --secondary-color: #00d2ff;
            --sidebar-bg: #2c3e50;
            --sidebar-hover: #34495e;
            --content-bg: #f8f9fa;
            --card-shadow: 0 4px 8px rgba(0,0,0,0.1);
            --primary: #3498db;
            --secondary: #2ecc71;
            --dark: #2c3e50;
            --light: #f8f9fa;
            --gray: #7f8c8d;
            --border: #e0e0e0;
            --card-bg: rgba(255, 255, 255, 0.95);
            --shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
            --error: #e74c3c;
            --success: #2ecc71;
        }

        body {

            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f0f2f5;
            color: #333;
            overflow-x: hidden;
        }

        .app-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            position: fixed; /* 固定定位 */
            top: 0;          /* 顶部对齐 */
            left: 0;         /* 左侧对齐 */
            height: 100vh;   /* 全屏高度 */
            overflow-y: auto; /* 允许侧边栏内容滚动 */
            width: 250px;
            background: var(--sidebar-bg);
            color: white;
            transition: all 0.3s;
            box-shadow: 3px 0 10px rgba(0,0,0,0.1);
            z-index: 1000;
        }

        .sidebar-header {
            padding: 20px;
            background: rgba(0,0,0,0.2);
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar-menu {
            padding: 15px 0;
        }

        .sidebar-menu li {
            margin-bottom: 5px;
        }

        .sidebar-menu a {
            color: #ecf0f1;
            padding: 12px 20px;
            display: block;
            text-decoration: none;
            transition: all 0.2s;
            border-radius: 4px;
            margin: 0 10px;
        }

        .sidebar-menu a:hover, .sidebar-menu a.active {
            background: var(--sidebar-hover);
            color: white;
        }

        .sidebar-menu a i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            padding: 20px;
            transition: all 0.3s;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid var(--border);
        }


        .back-btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 20px;
            background: var(--light);
            color: var(--dark);
            border: 1px solid var(--border);
            border-radius: 8px;
            font-weight: 500;
            cursor: pointer;
            transition: var(--transition);
            text-decoration: none;
        }

        .back-btn:hover {
            background: var(--primary);
            color: white;
            border-color: var(--primary);
        }
        .welcome-text h1 {
            font-size: 1.8rem;
            margin: 0;
            color: var(--primary-color);
        }

        .welcome-text p {
            margin: 5px 0 0;
            color: #666;
        }

        .user-profile {
            display: flex;
            align-items: center;
        }

        .user-info {
            text-align: right;
            margin-right: 15px;
        }

        .user-info .name {
            font-weight: 600;
            font-size: 1.1rem;
        }

        .user-info .role {
            font-size: 0.9rem;
            color: #777;
        }

        .avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 1.2rem;
        }

        /* 模块卡片样式 */
        .module-card {
            background: white;
            border-radius: 10px;
            box-shadow: var(--card-shadow);
            margin-bottom: 25px;
            overflow: hidden;
            transition: transform 0.3s;
        }

        .module-card:hover {
            transform: translateY(-5px);
        }

        .card-header {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-header h3 {
            margin: 0;
            font-size: 1.3rem;
        }

        .card-body {
            padding: 20px;
        }

        /* 业务表格 */
        .business-table {
            width: 100%;
            border-collapse: collapse;
        }

        .business-table th {
            background-color: #f8f9fa;
            padding: 12px 15px;
            text-align: left;
            font-weight: 600;
            color: #495057;
            border-bottom: 2px solid #e9ecef;
        }

        .business-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #e9ecef;
            color: #495057;
        }

        .business-table tr:hover {
            background-color: #f8f9fa;
        }


        /* 响应式设计 */
        @media (max-width: 992px) {
            .app-container {
                flex-direction: column;
            }



            .main-content {
                margin-left: 0;
            }
        }

        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                text-align: center;
            }

            .user-profile {
                margin-top: 15px;
                justify-content: center;
                width: 100%;
            }

            .user-info {
                text-align: center;
                margin-right: 0;
                margin-bottom: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 顶部标题栏 -->

            <div class="header">
            <button  class="back-btn" onclick="window.location.href='/company/index'">
                <i class="fas fa-arrow-left" ></i> 返回主页
            </button>
            </div>




            <!-- 业务列表模块 -->
            <div class="module-card">
                <div class="card-header">
                    <h3><i class="fas fa-briefcase me-2"></i> 业务列表</h3>
                    <div>
                        <button class="btn btn-sm btn-light" id="addBusinessBtn" onclick="window.location.href='/company/businessEdit'">
                            <i class="fas fa-plus me-1"></i> 添加业务
                        </button>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="business-table">
                            <thead>
                                <tr>
                                    <th>业务名称</th>
                                    <th>类型</th>
                                    <th>负责人</th>
                                    <th>开始日期</th>
                                    <th>结束日期</th>
                                    <th>状态</th>
                                    <th>优先级</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="businessList">
                                <!-- 业务数据将通过JavaScript动态加载 -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <script>
        // 业务数据
        let businessData = []; // 初始化为空数组

        // 页面加载时从后端获取数据
        document.addEventListener('DOMContentLoaded', function () {
            fetch('api/business') // 替换为你的实际接口地址
                .then(response => response.json())
                .then(data => {
                    businessData = data;
                    renderBusinessList();
                })
                .catch(error => {
                    console.error('获取业务数据失败:', error);
                    alert('无法加载业务数据，请检查网络或稍后再试');
                });
        });

        // 页面加载时初始化业务列表
        document.addEventListener('DOMContentLoaded', function() {
            renderBusinessList();
            setupEventListeners();
        });

        // 设置事件监听器
        function setupEventListeners() {
            // 保存业务按钮
            document.getElementById('saveBusinessBtn').addEventListener('click', saveBusiness);

            // 编辑业务按钮
            document.getElementById('editBusinessBtn').addEventListener('click', function() {
                const modal = bootstrap.Modal.getInstance(document.getElementById('businessDetailModal'));
                modal.hide();

                const detailModal = document.getElementById('businessDetailModal');
                const addModal = document.getElementById('addBusinessModal');

                // 将详情模态框中的数据复制到添加/编辑模态框
                document.getElementById('formBusinessName').value = document.getElementById('businessName').value;
                document.getElementById('formBusinessType').value = document.getElementById('businessType').value;
                document.getElementById('formResponsiblePerson').value = document.getElementById('responsiblePerson').value;
                document.getElementById('formStartDate').value = document.getElementById('startDate').value;
                document.getElementById('formEndDate').value = document.getElementById('endDate').value;
                document.getElementById('formBusinessStatus').value = document.getElementById('businessStatus').value;
                document.getElementById('formBusinessPriority').value = document.getElementById('businessPriority').value;
                document.getElementById('formBusinessDescription').value = document.getElementById('businessDescription').value;

                // 显示添加/编辑模态框
                bootstrap.Modal.getInstance(addModal).show();
            });
        }
        function formatDate(dateString) {
            if (!dateString) return '-';

            const date = new Date(dateString);
            if (isNaN(date)) return '-';

            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
            const day = String(date.getDate()).padStart(2, '0');

            return `${year}-${month}-${day}`;
        }

        // 渲染业务列表
        function renderBusinessList() {
            const businessListElement = document.getElementById('businessList');
            businessListElement.innerHTML = '';

            businessData.forEach(business => {
                const row = document.createElement('tr');

                row.innerHTML = `
            <td>${business.name || '-'}</td>
            <td>${business.type.name || '未知类型'}</td>
            <td>${business.owner.name || '未指定负责人'}</td>
            <td>${formatDate(business.startTime)}</td>
            <td>${formatDate(business.endTime)}</td>
            <td class="status-${getStatusClass(business.status)}">${business.status|| '未知状态'}</td>
            <td><span class="priority-${getPriorityClass(business.priority)}">${business.priority|| '未知优先级'}</span></td>
            <td>
                <button class="btn btn-sm btn-outline-info" onclick="showBusinessDetail(${business.id})">
                    <i class="fas fa-info-circle"></i> 详情
                </button>
                <button class="btn btn-sm btn-outline-warning ms-1" onclick="editBusiness(${business.id})">
                    <i class="fas fa-edit" ></i> 编辑
                </button>
                <button class="btn btn-sm btn-outline-danger ms-1" onclick="deleteBusiness(${business.id})">
                    <i class="fas fa-trash-alt"></i> 删除
                </button>
            </td>
        `;

                businessListElement.appendChild(row);
            });
        }


        // 获取状态类名
        function getStatusClass(status) {
            switch(status) {
                case '进行中':
                    return 'badge bg-success';
                case '未开始':
                    return 'badge bg-warning';
                case '已完成':
                    return 'badge bg-info';
                default:
                    return '';
            }
        }

        // 获取优先级类名
        function getPriorityClass(priority) {
            switch(priority) {
                case '高':
                    return 'high';
                case '中':
                    return 'medium';
                case '低':
                    return 'low';
                default:
                    return '';
            }
        }

        // 显示业务详情
        function showBusinessDetail(id) {
            const business = businessData.find(b => b.id === id);
            if (!business) return;

            document.getElementById('businessName').value = business.name;
            document.getElementById('businessType').value = business.type;
            document.getElementById('responsiblePerson').value = business.responsiblePerson;
            document.getElementById('startDate').value = business.startDate;
            document.getElementById('endDate').value = business.endDate;
            document.getElementById('businessStatus').value = business.status;
            document.getElementById('businessPriority').value = business.priority;
            document.getElementById('businessDescription').value = business.description;

            const modal = new bootstrap.Modal(document.getElementById('businessDetailModal'));
            modal.show();
        }

        // 编辑业务
        function editBusiness(id) {
            const business = businessData.find(b => b.id === id);
            const params = new URLSearchParams({
                id: business.id,
                name: business.name,
                typeId: business.type.id,
                typeName: business.type.name,
                ownerId: business.owner.id,
                ownerName: business.owner.name,
                startTime: business.startTime,
                endTime: business.endTime,
                status: business.status,
                priority: business.priority,
                description: business.description
            });
            window.location.href = '/company/businessEdit?' + params.toString();
        }

        // 删除业务
        function deleteBusiness(id) {
            if (confirm('确定要删除这个业务吗？')) {
                fetch('api/business/delete/' + id, {
                    method: 'DELETE',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                })
                    .then(response => {
                        if (response.ok) {
                            // 从前端数据中移除该条目
                            businessData = businessData.filter(b => b.id !== id);
                            renderBusinessList();
                            alert('业务删除成功');
                        } else {
                            throw new Error('删除失败');
                        }
                    })
                    .catch(error => {
                        console.error('删除业务时出错:', error);
                        alert('删除失败，请重试');
                    });
            }
        }


        // 保存业务
        function saveBusiness() {
            const id = parseInt(document.getElementById('businessId').value) || null;

            const business = {
                id: id || Date.now(),
                name: document.getElementById('formBusinessName').value,
                type: document.getElementById('formBusinessType').value,
                responsiblePerson: document.getElementById('formResponsiblePerson').value,
                startDate: document.getElementById('formStartDate').value,
                endDate: document.getElementById('formEndDate').value,
                status: document.getElementById('formBusinessStatus').value,
                priority: document.getElementById('formBusinessPriority').value,
                description: document.getElementById('formBusinessDescription').value
            };

            if (id) {
                // 更新现有业务
                const index = businessData.findIndex(b => b.id === id);
                if (index !== -1) {
                    businessData[index] = business;
                }
            } else {
                // 添加新业务
                businessData.push(business);
            }

            // 重置表单
            document.getElementById('businessForm').reset();
            document.getElementById('businessId').value = '';
            document.getElementById('addBusinessModalLabel').textContent = '添加业务';

            // 刷新列表
            renderBusinessList();

            // 关闭模态框
            const modal = bootstrap.Modal.getInstance(document.getElementById('addBusinessModal'));
            modal.hide();

            // 显示成功消息
            alert('业务保存成功！');
        }
    </script>
</body>
<!--111-->
</html>